/**
 * author      : dormi330
 * date        : 2018/6/24
 * project     : mybatis
 * description : 领域行为: 创建订单
 */

package com.deep.trade.domain.order;

import com.deep.trade.common.TradeException;
import com.deep.trade.infrastructure.utils.CommonUtils;
import com.deep.trade.order.InDtoAuditOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CommandAudit {

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());
    private static final int ONLY_ONE_SHOULD_BE_UPDATED = 1;


    @Autowired
    private OrderRepository orderRepository;

    /**
     * 新建订单
     *
     * @param tradeOrder      tradeOrder, 被审核的订单
     * @param inDtoAuditOrder 审核相关内容
     * @throws Exception
     */
    @Transactional
    public void auditOne(TradeOrder tradeOrder, InDtoAuditOrder inDtoAuditOrder) throws Exception {
        logger.info("{}, tradeOrder={}, inDtoAuditOrder={}", CommonUtils.currentClassAndMethod(), tradeOrder, inDtoAuditOrder);

        if (null == tradeOrder) throw new TradeException("order cannot be null");
        if (null == inDtoAuditOrder) throw new TradeException("inDtoAuditOrder cannot be null");

        tradeOrder.aduit(inDtoAuditOrder.isApprove(), inDtoAuditOrder.getRejectReason());

        if (ONLY_ONE_SHOULD_BE_UPDATED != orderRepository.updateTradeOrderStatus(tradeOrder)) {
            throw new TradeException("audit fail, cannot get order to audit");
        }
    }
}
